package com.yantong.service.impl;

import com.yantong.config.LocalRedis;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.ProductSku;
import com.yantong.mapper.ProductSkuMapper;
import com.yantong.service.ProductSkuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class ProductSkuServiceImpl implements ProductSkuService {
    private static final Logger logger = LoggerFactory.getLogger(ProductSkuServiceImpl.class);

    @Autowired
    private ProductSkuMapper skuMapper;
    @Autowired
    private LocalRedis localRedis;

    private static final String CACHE_SKU_NAMW = "PRODUCT_SKU_";

    @Override
    public PageUtils<ProductSku> list(int offset, int limit, String productName) {
        PageUtils<ProductSku> pageUtils = new PageUtils<>();

        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(productName)) {
            map.put("productName", productName);
        }
        int total = (int) skuMapper.countByWhere(map);
        if (total > 0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(skuMapper.listByWhere(map, new RowBounds(offset, limit)));
        } else {
            pageUtils.setRows(new ArrayList<>());
        }
        return pageUtils;
    }

    @Override
    public int save(ProductSku obj) {
        if (obj == null) {
            return 0;
        }
        return skuMapper.insertSelective(obj);
    }

    @Override
    public ProductSku getByKey(Long id) {
        if (id == null) {
            return null;
        }
        return skuMapper.selectByPrimaryKey(id);
    }

    @Override
    @CacheEvict(value = CACHE_SKU_NAMW, allEntries = true)
    public int update(ProductSku obj) {
        if (obj == null || obj.getId() == null) {
            return 0;
        }
        return skuMapper.updateByPrimaryKeySelective(obj);
    }

    @Override
    public int remove(Long id) {
        if (id == null) {
            return 0;
        }
        ProductSku obj = new ProductSku();
        obj.setId(id);
        obj.setOnlineStatus(0);
        return skuMapper.updateByPrimaryKeySelective(obj);
    }

    @Override
    @Cacheable(value = CACHE_SKU_NAMW, keyGenerator = "localKeyGenerator")
    public ProductSku getSkuByKeyWithCache(Long skuId) {
        if (skuId == null) {
            return null;
        }
        String key = CACHE_SKU_NAMW.concat(skuId.toString());
        ProductSku sku = localRedis.get(key, ProductSku.class);
        if (sku == null) {
            sku = skuMapper.selectByPrimaryKey(skuId);
            if (sku != null) {
                localRedis.put(key, sku);
            }
        }
        return sku;
    }

    @Override
    public int changeStock(int total, ProductSku sku) {
        return skuMapper.changeStock(total, sku.getId(), sku.getVersion());
    }

    @Override
    public int changeStock(int total, Long skuId) {
        return skuMapper.changeStock(total, skuId, null);
    }

    @Override
    public int refundStock(int total, ProductSku sku) {
        return skuMapper.refundStock(total, sku.getId(), sku.getVersion());
    }

}
